using System.Windows;
using System.Windows.Input;

namespace GameSaveManager;

/// <summary>
/// Interaction logic for SettingsWindow.xaml
/// </summary>
public partial class SettingsWindow : Window
{
    private AppSettings currentSettings = null!;
    private Key tempBackupKey;
    private ModifierKeys tempBackupModifiers;
    private Key tempRestoreKey;
    private ModifierKeys tempRestoreModifiers;

    public SettingsWindow()
    {
        InitializeComponent();
        LoadSettings();
    }

    private void LoadSettings()
    {
        // Load settings from file
        currentSettings = SettingsManager.Load();

        // Store in temporary variables
        tempBackupKey = currentSettings.BackupHotkeyKey;
        tempBackupModifiers = currentSettings.BackupHotkeyModifiers;
        tempRestoreKey = currentSettings.RestoreHotkeyKey;
        tempRestoreModifiers = currentSettings.RestoreHotkeyModifiers;

        // Display the formatted hotkeys
        BackupHotkeyTextBox.Text = FormatHotkey(tempBackupKey, tempBackupModifiers);
        RestoreHotkeyTextBox.Text = FormatHotkey(tempRestoreKey, tempRestoreModifiers);
    }

    private string FormatHotkey(Key key, ModifierKeys modifiers)
    {
        string result = "";

        if (modifiers.HasFlag(ModifierKeys.Control))
            result += "Ctrl + ";
        if (modifiers.HasFlag(ModifierKeys.Alt))
            result += "Alt + ";
        if (modifiers.HasFlag(ModifierKeys.Shift))
            result += "Shift + ";
        if (modifiers.HasFlag(ModifierKeys.Windows))
            result += "Win + ";

        result += key.ToString();

        return result;
    }

    private void BackupHotkeyTextBox_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
    {
        // Prevent default key input
        e.Handled = true;

        // Capture the key and modifiers
        Key key = e.Key;
        ModifierKeys modifiers = Keyboard.Modifiers;

        // Handle special keys that might be reported as System
        if (key == Key.System)
        {
            key = e.SystemKey;
        }

        // Store the captured combination
        tempBackupKey = key;
        tempBackupModifiers = modifiers;

        // Display the formatted combination
        BackupHotkeyTextBox.Text = FormatHotkey(key, modifiers);
    }

    private void RestoreHotkeyTextBox_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
    {
        // Prevent default key input
        e.Handled = true;

        // Capture the key and modifiers
        Key key = e.Key;
        ModifierKeys modifiers = Keyboard.Modifiers;

        // Handle special keys that might be reported as System
        if (key == Key.System)
        {
            key = e.SystemKey;
        }

        // Store the captured combination
        tempRestoreKey = key;
        tempRestoreModifiers = modifiers;

        // Display the formatted combination
        RestoreHotkeyTextBox.Text = FormatHotkey(key, modifiers);
    }

    private void SaveButton_Click(object sender, RoutedEventArgs e)
    {
        try
        {
            // Update settings with newly recorded hotkeys
            currentSettings.BackupHotkeyKey = tempBackupKey;
            currentSettings.BackupHotkeyModifiers = tempBackupModifiers;
            currentSettings.RestoreHotkeyKey = tempRestoreKey;
            currentSettings.RestoreHotkeyModifiers = tempRestoreModifiers;

            // Save to file
            SettingsManager.Save(currentSettings);

            DialogResult = true;
            Close();
        }
        catch (Exception ex)
        {
            System.Windows.MessageBox.Show($"Failed to save settings: {ex.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
        }
    }

    private void CancelButton_Click(object sender, RoutedEventArgs e)
    {
        DialogResult = false;
        Close();
    }
}
